<?php


namespace app\h7_tool\controller\pc;

use app\gm_arts\facade\service\MemberServiceFacade;
use app\gm_arts\facade\service\MenuServiceFacade;
use xsframe\base\BaseController;
use think\Request;
use think\App;

abstract class Base extends BaseController
{
    protected $pIndex;
    protected $pSize;

    protected $app;
    protected $header;
    protected $params;

    protected $siteRoot;
    protected $view;

    protected $controller;
    protected $action;
    protected $url;
    protected $rule;
    protected $iaRoot;

    public function __construct(Request $request, App $app)
    {
        parent::__construct($request, $app);

        $this->header = $this->request->header();
        $this->app    = $app;
        $this->params = $this->request->param();

        if (method_exists($this, '_home_initialize')) {
            $this->_home_initialize();
        }
    }

    // 初始化
    protected function _home_initialize()
    {
        $this->view       = $this->app['view'];
        $this->pIndex     = $this->request->param('page') ?? 1;
        $this->pSize      = $this->request->param('size') ?? ($this->request->param('limit') ?? 16);
        $this->rule       = $this->request->rule()->getRule();
        $this->_init();
    }

    // 初始化
    protected function _init()
    {
    }

    // 引入模板
    protected function template($name, $var = [])
    {
        $baseVar                = [];
        $baseVar['url']         = $this->url;
        $baseVar['rule']        = $this->rule;
        $baseVar['controller']  = $this->controller;
        $baseVar['action']      = $this->action;
        $baseVar['siteRoot']    = $this->siteRoot;
        $var['moduleSiteRoot']  = $this->moduleSiteRoot;
        $var['moduleAttachUrl'] = $this->moduleAttachUrl;

        $website = $this->moduleSetting['website'] ?? [];
        if (!empty($website)) {
            $website['logo']    = tomedia($website['logo']);
            $website['favicon'] = tomedia($website['favicon']);
        }
        $baseVar['website'] = $website;

        $baseVar['backUrl'] = urlencode(base64_encode($this->url));

        $var = array_merge($baseVar, $var);
        return view($name, $var); // TODO: Change the autogenerated stub
    }

    /**
     * 正确的数组数据
     * @param array $data
     * @param string $code
     * @param string $message
     * @return array
     */
    protected function success($data = [], $code = "200", $message = 'success')
    {
        $code    = $data['code'] ?? $code;
        $message = $data['msg'] ?? $message;
        $data    = $data['data'] ?? $data;

        $retData = [
            'code' => (string)$code,
            'msg'  => $message,
            'data' => $data
        ];
        return json($retData);
    }

    /**
     * 错误的数组数据
     * @param string $message
     * @param array $data
     * @param string $code
     * @return array
     */
    protected function error($message = 'fail', $code = "404")
    {
        $code    = $data['code'] ?? $code;
        $message = $data['msg'] ?? $message;

        $retData = [
            'code' => (string)$code,
            'msg'  => $message,
            'data' => [],
        ];
        die(json_encode($retData));
    }
}